import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewPage extends StatefulWidget {
  WebViewPage({Key key}) : super(key: key);

  @override
  _WebViewPageState createState() {
    return _WebViewPageState();
  }
}

class _WebViewPageState extends State<WebViewPage> {
  // WebViewController _controller;
  String title;
  InAppWebViewController webViewController;

  @override
  void initState() {
    super.initState();
    // if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
    onInit();
  }

  onInit() async {
    if (Platform.isAndroid) {
      await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);

      var swAvailable = await AndroidWebViewFeature.isFeatureSupported(
          AndroidWebViewFeature.SERVICE_WORKER_BASIC_USAGE);
      var swInterceptAvailable = await AndroidWebViewFeature.isFeatureSupported(
          AndroidWebViewFeature.SERVICE_WORKER_SHOULD_INTERCEPT_REQUEST);

      if (swAvailable && swInterceptAvailable) {
        AndroidServiceWorkerController serviceWorkerController =
            AndroidServiceWorkerController.instance();

        serviceWorkerController.serviceWorkerClient =
            AndroidServiceWorkerClient(
          shouldInterceptRequest: (request) async {
            print(request);
            return null;
          },
        );
      }
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var url = Get.arguments["url"];
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        elevation: .5,
        brightness: Brightness.light,
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.black),
        title: title == null
            ? CupertinoActivityIndicator()
            : Text(
                title,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 34.sp,
                ),
              ),
      ),
      body: Builder(builder: (BuildContext context) {
        return InAppWebView(
          initialOptions: InAppWebViewGroupOptions(
            crossPlatform: InAppWebViewOptions(
              // useShouldOverrideUrlLoading: false,
              // mediaPlaybackRequiresUserGesture: false,
              javaScriptEnabled: true,
              cacheEnabled: false,
              allowFileAccessFromFileURLs: true,

            ),
            android: AndroidInAppWebViewOptions(
              useHybridComposition: true,
              mixedContentMode:
              AndroidMixedContentMode.MIXED_CONTENT_ALWAYS_ALLOW,
              cacheMode: AndroidCacheMode.LOAD_NO_CACHE,
              allowFileAccess: true,
            ),
          ),
          onWebViewCreated: (controller) {
            webViewController = controller;
          },
          initialUrlRequest: URLRequest(url: Uri.parse(url)),
          onLoadStop: (controller, url) async {
            String title = await webViewController.getTitle();
            setState(() {
              this.title = title;
            });
          },
        );
      }),
    );
  }
}
